package org.jianfa.learn.transfer.service.impl;

import org.jianfa.learn.transfer.annotation.Autowired;
import org.jianfa.learn.transfer.annotation.Service;
import org.jianfa.learn.transfer.annotation.Transactional;
import org.jianfa.learn.transfer.dao.AccountDao;
import org.jianfa.learn.transfer.pojo.Account;
import org.jianfa.learn.transfer.service.TransferService;

@Service
public class TransferServiceImpl implements TransferService {

    // 最佳状态
    private AccountDao accountDao;

    // 构造函数传值/set方法传值
    @Autowired
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        accountDao.updateAccountByCardNo(to);
        accountDao.updateAccountByCardNo(from);
    }
}
